package com.zenchn.aliyun.oss.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Callback;

import java.io.*;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

/**
 * com.zenchn.aliyun.oss 公共数据组装类
 * @author dingxinfa
 * @date 2018-08-16
 *
 */
public class OssCommonUtils {

    public static Map<String,String> extractOSSException(OSSException ex) {
        ex.printStackTrace();
        Map<String, String> data = new HashMap<>();
        data.put("errorMessage", ex.getErrorMessage());
        data.put("errorCode", ex.getErrorCode());
        data.put("requestId", ex.getRequestId());
        data.put("hostId", ex.getHostId());
        return  data;
    }

    public static Map<String,String>  extractClientException(ClientException ex) {
        ex.printStackTrace();
        Map<String, String> data = new HashMap<>();
        data.put("errorMessage", ex.getErrorMessage());
        data.put("errorCode", ex.getErrorCode());
        data.put("requestId", ex.getRequestId());
        return  data;
    }


    public static Map<String,String>  extractIOException(IOException ex) {
        ex.printStackTrace();
        Map<String, String> data = new HashMap<>();
        data.put("errorMessage", ex.getMessage());
        return  data;
    }


    public static Map<String,String>  extractMalformedURLException(MalformedURLException ex) {
        ex.printStackTrace();
        Map<String, String> data = new HashMap<>();
        data.put("errorMessage", ex.getMessage());
        data.put("localizedMessage", ex.getLocalizedMessage());
        return  data;
    }



    public static Map<String,String>  extractThrowable(Throwable throwable) {
        throwable.printStackTrace();
        Map<String, String> data = new HashMap<>();
        data.put("errorMessage", throwable.getMessage());
        return  data;
    }


    /**
     * 组装建立上传回调参数集
     * @param callbackUrl
     * @param objectName
     * @return
     */
    public static Callback buildCallback(String callbackUrl, String objectName){
        // 上传回调参数。
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        // 设置发起回调时请求body的值。
        callback.setCallbackBody("{\"bucket\":${bucket},\"object\":${object},\"etag\":${etag},\"mimeType\":${mimeType},\"size\":${size},\"ossObjectKey\":${x:com.zenchn.aliyun.oss-object-key}}");
        // 设置发起回调请求的Content-Type。
        callback.setCalbackBodyType(Callback.CalbackBodyType.JSON);
        // 设置发起回调请求的自定义参数，由Key和Value组成，Key必须以x:开始。
        callback.addCallbackVar("x:com.zenchn.aliyun.oss-object-key", objectName);

        return  callback;
    }


    /**
     * 获得byte数组
     */

    public static byte[] getBytes(File file){
        byte[] buffer = null;
        try {
            InputStream inputStream=new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
            byte[] b = new byte[100];
            int n;
            while ((n = inputStream.read(b,0, 100)) > 0) {
                bos.write(b, 0, n);
            }
            inputStream.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }


    public static byte[] getBytes(InputStream inputStream){
        byte[] buffer = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
            byte[] b = new byte[100];
            int n;
            while ((n = inputStream.read(b,0, 100)) > 0) {
                bos.write(b, 0, n);
            }
            inputStream.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
